<template>
  <div class="cateList-container">
    <search-form
      :config-list="searchFormConfig"
      @search="search"
      @reset="reset"
      ref="searchFormRef"
    >
      <template #leftSlot>
        <!--左侧的插槽-->
      </template>
      <template #rightSlot>
        <!--右侧的插槽-->
      </template>
    </search-form>
    <CustomTable
      :pagination="pagination"
      :table-config="tableConfig"
      @pagination-change="paginationChange"
      @actions-click="actionsClick"
    />
    <storebyBrand ref="storebyBrandRef" @fetch-data="search" />
  </div>
</template>

<script>
  export default {
    name: 'storeby',
  }
</script>

<script setup>
  import CustomTable from '@/views/component/CustomTable'
  import SearchForm from '@/views/component/SearchForm' //引入搜索组件
  import { getStoreList as getList } from '~/src/api/store'
  import storebyBrand from './components/storeby.vue'
  import { useRouter, useRoute } from 'vue-router'
  const router = useRouter()
  const route = useRoute()

  // 搜索参数
  const queryForm = ref({})

  const tableConfig = reactive({
    columns: [
      {
        label: 'ID',
        prop: 'id',
        sortable: false,
        width: 80,
        fixed: 'left',
      },
      { label: '店铺名称', prop: 'name', sortable: false, width: 120 },
      { label: '联系电话', prop: 'mobile', sortable: false, width: 120 },
      { label: '登录账号', prop: 'account', sortable: false, width: 120 },
      { label: '店铺地址', prop: 'address', sortable: false, width: 220 },
      {
        label: '业务员',
        prop: 'sales_person_name',
        sortable: false,
        width: 120,
      },
      {
        label: '营业执照',
        prop: 'business_license',
        customType: 'image',
        sortable: false,
        width: 120,
        height: 120,
      },
      {
        label: '店铺logo',
        prop: 'logo',
        customType: 'image',
        sortable: false,
        width: 120,
        height: 120,
      },
      {
        label: '审核状态',
        prop: 'audit_status',
        sortable: false,
        width: 140,
        customType: 'ElTag',
        tagKeyValue: 'audit_status',
      },
      { label: '创建时间', prop: 'create_time', sortable: false, width: 180 },
      {
        label: '操作',
        action: true,
        fixed: 'right',
        actions: [
          {
            fnName: 'edit',
            title: '审核',
            type: 'primary',
            isTextBtn: true,
            permissionCode: 'storeAudit', // 权限
            key: 'audit_status',
            showValue: 1,
          },
          {
            fnName: 'view',
            title: '查看',
            type: 'primary',
            isTextBtn: true,
            permissionCode: 'storeView', // 权限
          },
          {
            fnName: 'setSalesman',
            title: '设置业务员',
            type: 'primary',
            isTextBtn: true,
            permissionCode: 'storeSetSalesman', // 权限
            key: 'audit_status',
            showValue: 2,
          },
          {
            fnName: 'setBrand',
            title: '关联品牌',
            type: 'primary',
            isTextBtn: true,
            permissionCode: 'storeSetBrand', // 权限
            key: 'audit_status',
            showValue: 2,
          },
          {
            fnName: 'setDiscount',
            title: '设置折扣',
            type: 'primary',
            isTextBtn: true,
            permissionCode: 'storeSetDiscount', // 权限
            key: 'audit_status',
            showValue: 2,
          },
        ],
      },
    ],
    tagConfig: {
      //审批状态 1:待审核 2:审批通过 3:审核不通过
      audit_status: {
        1: {
          name: '待审核',
          type: 'primary',
        },
        2: {
          name: '审批通过',
          type: 'success',
        },
        3: {
          name: '审核不通过',
          type: 'warning',
        },
      },
    },
    list: [],
    listLoading: false,
  })

  //搜索组件配置信息
  const searchFormConfig = reactive({
    config: {
      layoutType: 1, //1:左右布局,2:上下布局
      leftPanelWidth: 0, //控制左边布局控件宽度
    },
    controlList: [
      { label: '店铺名称', prop: 'name', type: 'input' },
      { label: '手机号', prop: 'mobile', type: 'input' },
      {
        label: '审核状态',
        prop: 'audit_status',
        type: 'select',
        list: Object.keys(tableConfig['tagConfig']['audit_status']).reduce(
          (acc, cur) => {
            const { name } = tableConfig['tagConfig']['audit_status'][cur]
            acc.push({ label: name, value: cur })
            return acc
          },
          []
        ),
      },
    ],
  })

  //点击搜索触发
  const search = (form) => {
    console.log(form)
    pagination.page = 1
    queryForm.value = form
    queryProject()
  }

  //列表分页参数
  const pagination = reactive({
    page: 1, //每页条数
    limit: 10, //当前页
    total: 0,
    layout: 'total, sizes, prev, pager, next, jumper',
  })

  //分页控件点击操作
  const paginationChange = (data) => {
    const { val, type } = data
    pagination[type] = val
    queryProject()
  }

  //请求列表数据
  const queryProject = () => {
    tableConfig.listLoading = true
    getList({
      ...queryForm.value,
      limit: pagination.limit,
      page: pagination.page,
    })
      .then((result) => {
        console.log('result', result)
        tableConfig.list = result.data.data
        tableConfig.listLoading = false
        pagination.total = result.data.total
      })
      .catch(() => {
        tableConfig.listLoading = false
      })
  }

  const storebyBrandRef = ref(null)

  //点击表格操作按钮回调
  const actionsClick = (data) => {
    console.log('actionsClick===', data)
    const { type, row } = data
    switch (type) {
      case 'edit':
        router.push({
          path: '/store/storeInfoApprove',
          query: { id: row.id, action: type },
        })
        break
      case 'view':
        router.push({
          path: '/store/storeInfoApprove',
          query: { id: row.id, action: type },
        })
        break
      default:
        storebyBrandRef.value.open(type, row)
        break
    }
  }

  const searchFormRef = ref(null)
  const reset = () => {
    if (!searchFormRef.value) return
    searchFormRef.value.resetFormFields()
    queryProject()
  }

  const init = async () => {
    await queryProject()
  }
  onActivated(() => {
    if (route.query.repalce) return init()
  })
  onMounted(() => {
    init()
  })
</script>

<style lang="scss" scoped>
  :deep(.btn_box) {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(60px, max-content));
    gap: 11px;
    justify-content: center;
    width: 100%;
  }
</style>
